
package com.neegix.development.api.interfaces.controller;

import com.neegix.base.PageVO;
import com.neegix.development.api.domain.entity.ApiEntity;
import com.neegix.exception.BusinessRuntimeException;
import com.neegix.result.Result;
import com.neegix.development.api.application.assembler.ApiAssembler;
import com.neegix.development.api.application.cqrs.query.ApiQueryRepository;
import com.neegix.development.api.interfaces.vo.ApiVO;
import com.neegix.development.api.application.service.ApiService;
import com.neegix.development.api.interfaces.form.NewApiForm;
import com.neegix.development.api.interfaces.form.QueryApiForm;
import com.neegix.development.api.interfaces.form.UpdateApiForm;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.access.prepost.PreAuthorize;

import java.util.List;
import java.util.Optional;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-11-22 11:43:14
  */

@RestController
@RequestMapping("/api/v1/api")
public class ApiController {

    @Autowired
    private ApiQueryRepository apiQueryRepository;

    @Autowired
    private ApiService apiService;

    @PreAuthorize("hasAuthority('development:api:add')")
    @PostMapping
    public Result<Void> createApi(@RequestBody @Valid NewApiForm apiForm){
        Void result = apiService.createApi(ApiAssembler.INSTANCE.covertEntity(apiForm));
        return Result.success("创建成功", result);
    }

    @PreAuthorize("hasAuthority('development:api:modify')")
    @PutMapping("/{id}")
    public Result<Void> updateApi(@PathVariable Long id, @RequestBody @Valid  UpdateApiForm apiForm){
        ApiEntity apiEntity = ApiAssembler.INSTANCE.covertEntity(apiForm);
        apiEntity.setId(id);
        Void result = apiService.modifyApi(apiEntity);
        return Result.success("更新成功",result);
    }

    @PreAuthorize("hasAuthority('development:api:list')")
    @GetMapping("/{currentPage}/{pageSize}")
    public Result<PageVO<ApiVO>> getApis(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, @ModelAttribute QueryApiForm apiForm){
        if(apiForm == null) {
            apiForm = new QueryApiForm();
        }
        PageVO<ApiVO> pageVO = apiQueryRepository.findPage(currentPage, pageSize, ApiAssembler.INSTANCE.covertDTO(apiForm));
        return Result.success("查询成功",pageVO);
    }

    @PreAuthorize("hasAuthority('development:api:get')")
    @GetMapping("/{id}")
    public Result<ApiVO> getApiById(@PathVariable("id") Long id) {
        Optional<ApiVO> optional = apiQueryRepository.findById(id);
        return Result.success("获取成功", optional.orElseThrow(()-> new BusinessRuntimeException("查询结果不存在")));
    }

    @PreAuthorize("hasAuthority('development:api:remove')")
    @DeleteMapping
    public Result<Void> removeApi(@RequestBody List<Long> ids){
        return Result.success("删除成功", apiService.removeApi(ids));
    }
}